
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "Celsus.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
	ON_WM_CLOSE()
	ON_COMMAND_RANGE(HLM_LRANGE, HLM_URANGE, OnHelpLink)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if( CFrameWnd::OnCreate(lpCreateStruct) == -1 )
		return -1;

	// create toolbar
	if( !m_wndToolBar.CreateEx( this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP ))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}
	// build the toolbar
	m_wndToolBar.BuildToolbar();

	// create rebar
	if( !m_wndReBar.Create(this) || !m_wndReBar.AddBar( &m_wndToolBar, NULL, NULL, RBBS_USECHEVRON ))
	{
		TRACE0("Failed to create rebar\n");
		return -1;      // fail to create
	}

	if( !m_wndStatusBar.Create( this ))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// set indicators
	m_wndStatusBar.SetIndicators( indicators, sizeof(indicators)/sizeof(UINT));

	// TODO: Remove this if you don't want tool tips
	m_wndToolBar.SetBarStyle( m_wndToolBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY );

	// get width
	INT nBand = 0;
	CRect rect;
	INT width = 0;
	// count buttons
	for( INT count = 0; count < m_wndToolBar.GetToolBarCtrl().GetButtonCount(); count++ )
	{
		if( !m_wndToolBar.GetToolBarCtrl().IsButtonHidden( m_wndToolBar.GetItemID( count )))
		{
			m_wndToolBar.GetToolBarCtrl().GetItemRect( count, rect );
			width += rect.Width();
		}
	}

	// set band info
	REBARBANDINFO rbbi;
	memset( &rbbi, NULL, sizeof(REBARBANDINFO) );

	rbbi.cbSize = sizeof(REBARBANDINFO);
	rbbi.fMask = RBBIM_CHILDSIZE | RBBIM_IDEALSIZE | RBBIM_SIZE | RBBIM_ID;
	rbbi.cxMinChild = 0;
	rbbi.cyMinChild = rect.Height();		
	rbbi.cxIdeal = width + 10;
	rbbi.cx = rbbi.cxIdeal + 15;
	rbbi.wID = ID_TOOLBAR;
	m_wndReBar.GetReBarCtrl().SetBandInfo(nBand, &rbbi);

	// set options
	if( appConfig.m_Options.bDisableMyView )
		GetMenu()->DeleteMenu( ID_REQUEST_CLAIM, MF_BYCOMMAND );
	if( appConfig.m_Options.bDisableFileSendExternally )
		GetMenu()->DeleteMenu( ID_FILE_SEND_EXTERNALLY, MF_BYCOMMAND );
	if( appConfig.m_Options.bDisableSourceLinkCheck )
	{
		GetMenu()->DeleteMenu( ID_VIEW_LINKSLIST, MF_BYCOMMAND );
		GetMenu()->DeleteMenu( ID_VIEW_SOURCELINKSLIST, MF_BYCOMMAND );
	}

	// add help links
	CStringArray csaHelpLinks;
	appConfig.GetHelpLinks( csaHelpLinks );
	INT nCount = csaHelpLinks.GetCount();
	if( nCount > 0 )
	{
		CString szItem;
		CMenu *pMenu = GetMenu()->GetSubMenu( HELP_MENU_ITEM );
		// add separator
		pMenu->AppendMenu( MF_SEPARATOR );
		// add the Help Link items to the Help menu
		for( INT nIndex=0; nIndex < nCount; nIndex++ )
		{
			// get help link name
			szItem = csaHelpLinks[nIndex].Left( csaHelpLinks[nIndex].Find( _T( "=" ) ));
			pMenu->AppendMenu( MF_STRING, HLM_LRANGE + nIndex, szItem );
			// get help link URL
			szItem = csaHelpLinks[nIndex].Right( csaHelpLinks[nIndex].GetLength() - ( csaHelpLinks[nIndex].Find( _T( "=" )) + 1 ));
			m_csaHelpLinkURL.Add( szItem );
		}
	}

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}
#endif //_DEBUG


void CMainFrame::GetMessageString(UINT nID, CString& rMessage) const
{
	// check if it is a Help Link
	if( nID >= HLM_LRANGE && nID <= HLM_URANGE )
	{
		rMessage.Format( _T("Open Browser to: %s"), m_csaHelpLinkURL[nID - HLM_LRANGE] );
		return;
	}

	return CFrameWnd::GetMessageString(nID, rMessage);
}

// CMainFrame message handlers

void CMainFrame::OnClose()
{
	// check if we are created
	if( !IsZoomed() )
	{
		CRect rect;
		GetWindowRect( rect );	
		// save loaction
		appConfig.WriteWindowLocation( _T("Main_Window"), rect );
	}
	CFrameWnd::OnClose();
}

BOOL CMainFrame::SetStatus( INT nIndicator, LPCTSTR lpszNewText )
{
	// set text
	return m_wndStatusBar.SetPaneText( nIndicator, lpszNewText );
}

void CMainFrame::OnHelpLink(UINT nID)
{
	// open URL in default browser
	ShellExecute( NULL, _T("open"), m_csaHelpLinkURL[nID - HLM_LRANGE], NULL, NULL, SW_SHOWNORMAL );
}
